package com.jinke.api.common.util;

import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.util.encoders.Hex;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @projectName: jinke-api
 * @title: SHA256Util
 * @description: SHA256加密
 * @see: jinke-api
 * @author: hhp
 * @version: 1.0.0
 * @createTime: 2024/8/23 14:46
 */
public class SHA256Util {
    public static String sha256(String param) {
        return Hex.toHexString(bytes(param));
    }

    public static byte[] bytes(String content) {
        if (content == null || content.isEmpty()) {
            throw new IllegalArgumentException();
        }
        SHA256Digest sha256Digest = new SHA256Digest();
        byte[] bytes = content.getBytes();
        sha256Digest.update(bytes, 0, bytes.length);
        byte[] bytes1 = new byte[sha256Digest.getDigestSize()];
        sha256Digest.doFinal(bytes1, 0);
        return bytes1;
    }
}
